<template>
<div class="content">
  <template v-if="loading">
    <div
      v-for="i in 6"
      :key="i"
      class="card-skeleton"
    >
      <el-skeleton style="width: 13vw; height: 28px;" variant="image" />
    </div>
  </template>
  <template v-else>
    <div class="tab">
      <div class="tab-item" @click="currentTab = '好评榜'" :class="{ active: currentTab === '好评榜' }">好评榜</div>
      <div class="tab-item" @click="currentTab = '销量榜'" :class="{ active: currentTab === '销量榜' }">销量榜</div>
    <!-- </div>
    <div class="scroll-container">
      <div v-if="currentTab === '好评榜'" class="scroll-content">
        <el-card
          v-for="item in sortedBySalesVolume"
          :key="item.name"
          class="card"
          body-style="padding: 5px;"
        >
          <div style="display: flex">
            <div><img :src="item.imgUrl" class="image" /></div>
            <div style="text-align: center">
              <div><el-text class="name">{{ item.name }}</el-text></div>
              <div><el-text class="price">{{ item.price.toFixed(2) }}</el-text></div>
            </div>
            <div>
              <img src="@/assets/ranking/金牌.png" width="30px"/>
            </div>
          </div>
          <div class="text-area">
            <el-text class="quantity">已有{{ item.salesVolume }}人好评！</el-text>
            <el-text class="score">{{ item.score }}</el-text>
          </div>
        </el-card>
      </div>
      <div v-if="currentTab === '销量榜'" class="scroll-content">
        <el-card
          v-for="item in sortedBySalesVolume"
          :key="item.name"
          class="card"
          body-style="padding: 5px;"
        >
          <div style="display: flex">
            <div><img :src="item.imgUrl" class="image" /></div>
            <div style="text-align: center">
              <div><el-text class="name">{{ item.name }}</el-text></div>
              <div><el-text class="price">{{ item.price.toFixed(2) }}</el-text></div>
            </div>
            <div>
              <img src="@/assets/ranking/金牌.png" width="30px"/>
            </div>
          </div>
          <div class="text-area">
            <el-text class="quantity">已有{{ item.salesVolume }}人好评！</el-text>
            <el-text class="score">{{ item.score }}</el-text>
          </div>
        </el-card>
      </div>
    </div> -->
  </template>
</div>
</template>

<script lang="ts" setup>
import { ref, computed } from 'vue';
import type { Goods } from '@/common/types/result';

interface Props {
  ListItem: Goods[];
}

const props = defineProps<Props>();
const loading = ref(false);
const currentTab = ref('好评榜');

const setLoading = () => {
  loading.value = true;
  setTimeout(() => {
    loading.value = false;
  }, 2000);
};

setLoading();

const sortedBySalesVolume = computed(() => {
  return [...props.ListItem].sort((a, b) => b.salesVolume - a.salesVolume);
});

const sortedByScore = computed(() => {
  return [...props.ListItem].sort((a, b) => b.score - a.score);
});
</script>

<style scoped lang="scss">
.content {
  width: 1200px;
  height: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin: 0 auto;
}

.tab {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-bottom: 10px;
}

.tab-item {
  padding: 5px 10px;
  cursor: pointer;
  background-color: #f2f2f2;
  border-radius: 5px;
  font-weight: bold;
  transition: background-color 0.3s;
}

.tab-item.active {
  background-color: #42b983;
  color: white;
}

.scroll-container {
  overflow-x: auto;
  white-space: nowrap;
  padding: 10px;
}

.scroll-content {
  display: inline-block;
}

.card {
  width: 180px;
  box-shadow: none;
  font-size: 12px;
  display: inline-block;
  margin: 2px;
}

img {
  margin-left: 14px;
}

.card-skeleton {
  width: 180px;
  display: inline-block;
}

span {
  font-size: 12px;
}

.name {
  font-weight: bold;
}

.image {
  width: 50px;
  object-fit: cover;
}

.score {
  width: 24px;
  height: 16px;
  font-size: 15px;
  color: red;
  line-height: 10px;
  text-align: center;
  padding-left: 14px;
}
</style>
